import java.util.Random;
//老师给我改的版本

public class Test4 {
    public static void main(String[] args) {
        Object lock = new Object();
        //创建一个队列
        MyBlockingQueue myBlockingQueue = new MyBlockingQueue();
        Thread customer = new Thread(()->{
            while(true){
                try {
                    synchronized (lock){
                        if (myBlockingQueue.getSize() == 0){
                            lock.wait();
                        }else{
                            int cust = myBlockingQueue.take();
                            System.out.println("消费了:"+cust+"，当前元素个数："+myBlockingQueue.getSize());
                            lock.notify();
                        }
                    }
                    //Thread.sleep(3000);
                } catch (InterruptedException e) {
                    //throw new RuntimeException(e);
                    e.printStackTrace();
                }
            }

        },"customer");
        customer.start();
        Thread producer = new Thread(()->{
            Random random = new Random();
            while(true){
                int product = random.nextInt(100);
                try {
                    synchronized (lock){
                        if (myBlockingQueue.getSize() >= 1000){
                            lock.wait();
                        }else{
                            myBlockingQueue.put(product);
                            System.out.println("产生了："+product+"，当前元素个数："+myBlockingQueue.getSize());
                            lock.notify();
                        }
                    }
                    //Thread.sleep(3000);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }

            }
        },"producer");
        producer.start();
    }
}